{#

This file is part of the Sonata package.

(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

#}

{% extends sonata_block.templates.block_base %}

{% block block %}
    {% set results_count = pager ? pager.countResults() : 0 %}
    {% set has_results = results_count > 0 %}
    {% set current_page_results = has_results ? pager.currentPageResults : [] %}
    {% set visibility_class = 'sonata-search-result-' ~ (has_results ? 'show' : show_empty_boxes) %}

    <div class="col-lg-4 col-md-6 search-box-item {{ visibility_class }}">
        <div class="box box-solid {{ visibility_class }}">
            <div class="box-header with-border {{ visibility_class }}">
                {{ settings.icon|default('')|parse_icon }}
                <h3 class="box-title">
                    {% if admin.label is not empty %}
                        {{ admin.label|trans({}, admin.translationdomain) }}
                    {% endif %}
                </h3>

                <div class="box-tools pull-right">
                    {% if has_results %}
                        <span class="badge">{{ results_count }}</span>
                    {% elseif admin.hasRoute('create') and admin.hasAccess('create') %}
                        <a href="{{ admin.generateUrl('create') }}" class="btn btn-box-tool">
                            <i class="fas fa-plus" aria-hidden="true"></i>
                        </a>
                    {% endif %}
                    {% if admin.hasRoute('list') and admin.hasAccess('list') %}
                        <a href="{{ admin.generateUrl('list') }}" class="btn btn-box-tool">
                            <i class="fas fa-list" aria-hidden="true"></i>
                        </a>
                    {% endif %}
                </div>

                <div class="matches">
                    {% for name, filter in filters %}
                        <a class="label label-primary" href="{{ admin.generateUrl('list', {'filter': {(filter.formName): {'value': term}}}) }}">
                            {% if filter.translationDomain is same as(false) %}
                                {{ filter.label }}
                            {% else %}
                                {{ filter.label|trans({}, filter.translationDomain ?? admin.translationDomain) }}
                            {% endif %}
                        </a>
                    {% endfor %}
                </div>
            </div>
            {% if has_results %}
                <div class="box-body no-padding">
                    <ul class="nav nav-stacked sonata-search-result-list">
                        {% for result in current_page_results %}
                            {% if admin.hasRoute(admin_route) and admin.hasAccess(admin_route, result) %}
                                <li>
                                    <a href="{{ admin.generateObjectUrl(admin_route, result) }}">
                                        {{ admin.toString(result) }}
                                    </a>
                                </li>
                            {% else %}
                                <li><a>{{ admin.toString(result) }}</a></li>
                            {% endif %}
                        {% endfor %}
                    </ul>
                </div>
            {% else %}
                <div class="box-body">
                    <p>
                        <em>{{ 'no_results_found'|trans({}, 'SonataAdminBundle') }}</em>
                    </p>
                </div>
            {% endif %}
        </div>
    </div>
{% endblock %}
